<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>退出登录 - 湘行</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Noto Serif SC', serif;
        }

        body {
            background: linear-gradient(135deg, #f5f1e9 0%, #e6dfd2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .logout-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(139, 69, 19, 0.15);
            width: 100%;
            max-width: 500px;
            overflow: hidden;
            position: relative;
        }

        .logout-header {
            background: linear-gradient(to right, #8B4513, #A67C52);
            color: white;
            padding: 25px 30px;
            text-align: center;
            position: relative;
        }

        .logout-header h1 {
            font-family: 'Ma Shan Zheng', cursive;
            font-size: 28px;
            margin-bottom: 5px;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
        }

        .logout-header p {
            opacity: 0.9;
            font-size: 15px;
        }

        .logout-content {
            padding: 30px;
            text-align: center;
        }

        .user-info {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 25px;
        }

        .user-avatar {
            width: 70px;
            height: 70px;
            border-radius: 50%;
            background-image: url('https://placekitten.com/200/200');
            background-size: cover;
            background-position: center;
            border: 3px solid #D2B48C;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
        }

        .user-details {
            margin-left: 15px;
            text-align: left;
        }

        .user-details h2 {
            color: #8B4513;
            font-size: 20px;
            margin-bottom: 5px;
        }

        .user-details p {
            color: #666;
            font-size: 14px;
        }

        .confirmation-text {
            color: #5D4037;
            margin-bottom: 30px;
            line-height: 1.6;
            font-size: 16px;
        }

        .button-group {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }

        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 50px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            min-width: 120px;
        }

        .btn-logout {
            background: linear-gradient(to right, #9E2A2B, #CC5500);
            color: white;
            box-shadow: 0 4px 15px rgba(158, 42, 43, 0.3);
        }

        .btn-logout:hover {
            background: linear-gradient(to right, #CC5500, #9E2A2B);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(158, 42, 43, 0.4);
        }

        .btn-cancel {
            background: #f1f1f1;
            color: #666;
            border: 1px solid #ddd;
        }

        .btn-cancel:hover {
            background: #e1e1e1;
        }

        .logout-progress {
            display: none;
            margin-top: 25px;
        }

        .progress-bar {
            height: 6px;
            background: #f0f0f0;
            border-radius: 3px;
            overflow: hidden;
            margin-bottom: 15px;
        }

        .progress-fill {
            height: 100%;
            width: 0%;
            background: linear-gradient(to right, #8B4513, #A67C52);
            border-radius: 3px;
            transition: width 0.5s ease;
        }

        .progress-text {
            color: #8B4513;
            font-size: 14px;
        }

        .logout-success {
            display: none;
            text-align: center;
            padding: 20px;
            background: #f0fff4;
            border-radius: 8px;
            margin-top: 20px;
            border-left: 4px solid #38a169;
        }

        .logout-success h3 {
            color: #38a169;
            margin-bottom: 10px;
        }

        .logout-footer {
            margin-top: 30px;
            text-align: center;
            color: #888;
            font-size: 13px;
            padding: 15px;
            border-top: 1px solid #eee;
        }

        .decoration {
            position: absolute;
            opacity: 0.1;
        }

        .decoration-1 {
            top: -20px;
            right: -20px;
            width: 150px;
            height: 150px;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cpath d='M50 10 L72 28 L80 55 L62 72 L35 80 L18 62 L10 35 L28 18 Z' fill='%238B4513'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
        }

        .decoration-2 {
            bottom: -30px;
            left: -30px;
            width: 200px;
            height: 200px;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cpath d='M20 50 Q50 10 80 50 Q50 90 20 50' fill='%239E2A2B'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
        }

        .fade-in {
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
    <div class="logout-container fade-in">
        <div class="decoration decoration-1"></div>
        <div class="decoration decoration-2"></div>

        <div class="logout-header">
            <h1>湘行</h1>
            <p>湘西旅游专家</p>
        </div>

        <div class="logout-content">
            <div class="user-info">
                <div class="user-avatar"></div>
                <div class="user-details">
                    <h2>湘西旅行者</h2>
                    <p>xiangxi@example.com</p>
                </div>
            </div>

            <div class="confirmation-text">
                <p>您确定要退出登录吗？退出后需要重新登录才能访问您的行程、收藏和个人信息。</p>
            </div>

            <div class="button-group">
                <button class="btn btn-cancel" id="cancelBtn">取消</button>
                <button class="btn btn-logout" id="logoutBtn">退出登录</button>
            </div>

            <div class="logout-progress" id="logoutProgress">
                <div class="progress-bar">
                    <div class="progress-fill" id="progressFill"></div>
                </div>
                <p class="progress-text" id="progressText">正在退出登录，请稍候...</p>
            </div>

            <div class="logout-success" id="logoutSuccess">
                <h3>退出成功！</h3>
                <p>您已安全退出登录，即将跳转到首页...</p>
            </div>
        </div>

        <div class="logout-footer">
            <p>© 2023 湘行 - 探索湘西之美</p>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const logoutBtn = document.getElementById('logoutBtn');
            const cancelBtn = document.getElementById('cancelBtn');
            const logoutProgress = document.getElementById('logoutProgress');
            const logoutSuccess = document.getElementById('logoutSuccess');
            const progressFill = document.getElementById('progressFill');
            const progressText = document.getElementById('progressText');

            // 取消按钮 - 返回上一页
            cancelBtn.addEventListener('click', function() {
                window.history.back();
            });

            // 退出登录按钮
            logoutBtn.addEventListener('click', function() {
                // 隐藏按钮，显示进度条
                document.querySelector('.button-group').style.display = 'none';
                logoutProgress.style.display = 'block';

                // 模拟进度条动画
                let progress = 0;
                const interval = setInterval(function() {
                    progress += 5;
                    progressFill.style.width = progress + '%';

                    if (progress >= 100) {
                        clearInterval(interval);
                        progressText.textContent = '退出成功！';

                        // 执行退出操作
                        performLogout();
                    }
                }, 100);
            });

            // 执行退出登录的函数
            // 替换原performLogout函数
    function performLogout() {
        // 1. 先调用后端退出接口销毁会话
        fetch('/user/logout/api/', {
            method: 'POST',
            credentials: 'include', // 关键：携带Cookie以销毁后端会话
            headers: {
                'X-CSRFToken': getCookie('csrftoken'), // Django需要CSRF令牌
            }
        }).then(response => response.json())
          .then(data => {
              if (data.status === 'success') {
                  // 2. 清除本地存储
                  clearUserData();

                  // 3. 显示成功消息并跳转
                  setTimeout(function() {
                      logoutProgress.style.display = 'none';
                      logoutSuccess.style.display = 'block';

                      setTimeout(function() {
                          window.location.href = '/search/'; // 跳转后模板会重新判断登录状态
                      }, 2000);
                  }, 500);
              }
          });
    }

    // 辅助函数：获取CSRF令牌（Django需要）
    function getCookie(name) {
        let cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            const cookies = document.cookie.split(';');
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

            // 清除用户数据
            function clearUserData() {
                // 清除本地存储中的用户数据
                localStorage.removeItem('userToken');
                localStorage.removeItem('userInfo');
                sessionStorage.removeItem('userSession');

                // 清除Cookie中的用户数据
                document.cookie = 'auth_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';

                // 在实际项目中，这里还应该调用后端API通知服务器退出登录
                console.log('用户数据已清除');
            }
        });
    </script>
</body>
</html>